package cn.ecnu.controller.personal;

import cn.ecnu.common.Relation;
import cn.ecnu.common.utils.R;
import cn.ecnu.mapper.personal.ContactMapper;
import cn.ecnu.pojo.personal.Contact;
import cn.ecnu.pojo.personal.Title;
import cn.ecnu.pojo.recommend.RecommendJob;
import cn.ecnu.pojo.resume.ResumeExp;
import cn.ecnu.service.personal.ContactService;
import cn.ecnu.service.personal.TitleService;
import cn.ecnu.service.recommend.RecommendJobService;
import cn.ecnu.service.resume.ResumeExpService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@Validated
@RequestMapping("/contact")
public class ContactController {

    @Resource
    TitleService titleService;
    @Resource
    ContactService contactService;
    @Resource
    ResumeExpService resumeExpService;
    @Resource
    RecommendJobService recommendJobService;
    @Resource
    ContactMapper contactMapper;

    @GetMapping("/candidate/mine")
    public R getMyRecommenders(@RequestParam("userId") Integer userId) {
        List<Title> titles = contactMapper.listSpecificTitles("candidate_id","recommender_id", userId, Relation.normal.ordinal());
        return R.data(titles);
    }

    @GetMapping("/candidate/wantme")
    public R getInterestedRecommenders(@RequestParam("userId") Integer userId) {
        List<Title> titles = contactMapper.listSpecificTitles("candidate_id","recommender_id", userId, Relation.R2C.ordinal());
        return R.data(titles);
    }

    @GetMapping("/recommender/mine")
    public R getMyCandidates(@RequestParam("userId") Integer userId) {
        List<Title> titles = contactMapper.listSpecificTitles("recommender_id","candidate_id", userId, Relation.normal.ordinal());
        return R.data(titles);
    }

    @GetMapping("/recommender/wantme")
    public R getInterestedCandidates(@RequestParam("userId") Integer userId) {
        List<Title> titles = contactMapper.listSpecificTitles("recommender_id","candidate_id", userId, Relation.C2R.ordinal());
        return R.data(titles);
    }


    public boolean changeRelation(Contact change, Relation ourCurRelation, Relation targetRelation) {
        Contact contact;
        int candidateId = change.getCandidateId();
        int recId = change.getRecommenderId();
        if ((contact = contactService.getOne(Wrappers.<Contact>lambdaQuery()
                .eq(Contact::getCandidateId, candidateId)
                .eq(Contact::getRecommenderId, recId)
                .eq(Contact::getRelation, ourCurRelation.ordinal()))) == null) {
            return false;
        }
        contact.setRelation(targetRelation.ordinal());
        contactService.update(contact,Wrappers.<Contact>lambdaUpdate()
            .eq(Contact::getCandidateId,candidateId)
            .eq(Contact::getRecommenderId,recId));
        return true;
    }

    @PutMapping("/candidate/agree")
    public R agreeRecommender(@RequestBody Contact change) {
        if (changeRelation(change, Relation.R2C, Relation.normal)) {
            return R.ok();
        }
        return R.error("通过失败");
    }

    @PutMapping("/candidate/disagree")
    public R disagreeRecommender(@RequestBody Contact change) {
        if (changeRelation(change, Relation.R2C, Relation.CDis)) {
            return R.ok();
        }
        return R.error("失败");
    }

    @PutMapping("/recommender/agree")
    public R agreeCandidate(@RequestBody Contact change) {
        if (changeRelation(change, Relation.C2R, Relation.normal)) {
            return R.ok();
        }
        return R.error("通过失败");
    }

    @PutMapping("/recommender/disagree")
    public R disagreeCandidate(@RequestBody Contact change) {
        if (changeRelation(change, Relation.C2R, Relation.RDis)) {
            return R.ok();
        }
        return R.error("失败");
    }


    /**
     * 求职者想添加内推官
     * @param contact 两个用户的id
     */
    @PostMapping("/candidate/want_recommender")
    public R candidateWant(@RequestBody Contact contact){
        contact.setRelation(Relation.C2R.ordinal());
        Contact one = contactService.getOne(Wrappers.<Contact>lambdaQuery()
                .eq(Contact::getCandidateId, contact.getCandidateId())
                .eq(Contact::getRecommenderId, contact.getRecommenderId()));
        if (one==null ){
            contactService.save(contact);
            return R.ok();
        }
        if (one.getRelation() == Relation.R2C.ordinal()) {
            contact.setRelation(Relation.normal.ordinal());
        }
        contactService.saveOrUpdate(contact, Wrappers.<Contact>lambdaUpdate()
                .eq(Contact::getCandidateId,contact.getCandidateId())
                .eq(Contact::getRecommenderId,contact.getRecommenderId()));
        return R.ok();
    }

    /**
     * 内推官想添加求职者
     * @param contact 俩用户的id
     */
    @PostMapping("/recommender/want_candidate")
    public R recommenderWant(@RequestBody Contact contact){
        contact.setRelation(Relation.R2C.ordinal());
        Contact one = contactService.getOne(Wrappers.<Contact>lambdaQuery()
                .eq(Contact::getCandidateId, contact.getCandidateId())
                .eq(Contact::getRecommenderId, contact.getRecommenderId()));
        if (one==null ){
            contactService.save(contact);
            return R.ok();
        }
        if (one.getRelation() == Relation.C2R.ordinal()) {
            contact.setRelation(Relation.normal.ordinal());
        }
        contactService.saveOrUpdate(contact, Wrappers.<Contact>lambdaUpdate()
                .eq(Contact::getCandidateId,contact.getCandidateId())
                .eq(Contact::getRecommenderId,contact.getRecommenderId()));
        return R.ok();
    }
}
